package com.yswl.pri.home.vedio;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient;
import com.tencent.smtt.export.external.interfaces.JsResult;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.export.external.interfaces.WebResourceResponse;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.tencent.smtt.utils.TbsLog;
import com.yswl.pri.common.BaseBrowserActivity;
import com.yswl.pri.home.vedio.utils.AnalysUtil;
import com.yswl.pri.R;
import com.yswl.pri.home.vedio.utils.MInfoManager;
import com.yswl.pri.home.vedio.utils.Route;

import java.util.List;

import yswl.com.klibrary.util.L;

public class BrowserActivity extends BaseBrowserActivity {

    private static final String TAG = BrowserActivity.class.getSimpleName();

    public static void JumpAct(Context context, String title, String url) {
        Intent intent = new Intent(context, BrowserActivity.class);
        intent.putExtra("video_url", url);
        intent.putExtra("video_name", title);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
        intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        context.startActivity(intent);
    }


    private static String mSiteUrl;

    private List<Route> routeList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        routeList = MInfoManager.getMInfo().routeList;
        //获取站点
        mSiteUrl = getIntent().getStringExtra("video_url");
        L.e(TAG, " 网页视频地址 :" + mSiteUrl);
        setToolbarTitle(getIntent().getStringExtra("video_name"));
        long time = System.currentTimeMillis();
        if (mSiteUrl != null) {
            //有可能带入过来的url已经加了解析地址
            String newUrl;
            if (!AnalysUtil.G(mSiteUrl)) {
                //拼接成能解析播放的地址
                newUrl = routeList.get(0).url + mSiteUrl;
            } else {
                newUrl = mSiteUrl;
            }
            Log.e(TAG, " 解析后视频地址 :" + newUrl);
            mWebView.loadUrl(newUrl);
            mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
            mWebView.requestFocus();
            mWebView.setWebChromeClient(getCustomWebChromeClient());
            mWebView.setWebViewClient(getCustomWebViewClient());
        }

        TbsLog.d("time-cost", "cost time: " + (System.currentTimeMillis() - time));
    }


     WebChromeClient getCustomWebChromeClient() {
        return new MyWebChromeClient();
    }

    WebViewClient getCustomWebViewClient() {
        return new MyWebviewClient();
    }

    class MyWebviewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String url) {
            //拦截广告返回true 不让其跳转
            if (AnalysUtil.GG(url, 1)) {
                return true;
            }
            Log.e(TAG, "shouldOverrideUrlLoading :" + url);
            return false;
        }

        @Override
        public WebResourceResponse shouldInterceptRequest(WebView webView, WebResourceRequest webResourceRequest) {
            String url = webResourceRequest.getUrl().toString();
            //解析网站地址 不能过滤
//            if (AnalysUtil.G(url)) {
//                return super.shouldInterceptRequest(webView, webResourceRequest);
//            }
//            L.e(TAG, "第一层 可能包含广告的网站地址 shouldInterceptRequest :" + url);
//            if (AnalysUtil.GG(url, 1)) {
//                return new WebResourceResponse();
//            }
//            L.e(TAG, "第三层 过滤后广告后网站 shouldInterceptRequest :" + url);
            return super.shouldInterceptRequest(webView, webResourceRequest);
        }


        @Override
        public void onLoadResource(WebView webView, String url) {
            L.e(TAG, "onLoadResource :" + url);
            super.onLoadResource(webView, url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            L.e(TAG, "onPageFinished :" + url);
            super.onPageFinished(view, url);
        }
    }

    class MyWebChromeClient extends WebChromeClient {
        View myVideoView;
        View myNormalView;
        IX5WebChromeClient.CustomViewCallback callback;

        /**
         * 全屏播放配置
         */
        @Override
        public void onShowCustomView(View view, IX5WebChromeClient.CustomViewCallback customViewCallback) {
            FrameLayout normalView = (FrameLayout) findViewById(R.id.web_filechooser);
            ViewGroup viewGroup = (ViewGroup) normalView.getParent();
            viewGroup.removeView(normalView);
            viewGroup.addView(view);
            myVideoView = view;
            myNormalView = normalView;
            callback = customViewCallback;
        }

        @Override
        public boolean onJsAlert(WebView webView, String s, String s1, JsResult jsResult) {
            return true;
        }

        @Override
        public void onHideCustomView() {
            if (callback != null) {
                callback.onCustomViewHidden();
                callback = null;
            }
            if (myVideoView != null) {
                ViewGroup viewGroup = (ViewGroup) myVideoView.getParent();
                viewGroup.removeView(myVideoView);
                viewGroup.addView(myNormalView);
            }
        }

        @Override
        public void onProgressChanged(WebView webView, int newProgress) {
            super.onProgressChanged(webView, newProgress);
            if (null != mPageLoadingProgressBar)
                mPageLoadingProgressBar.setProgress(newProgress);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.clear();
        SubMenu sub = menu.addSubMenu(1, 100, 0, "切换线路").setIcon(R.mipmap.icon_change);
        sub.add(1, 111, 0, "刷新");
        for (int i = 0; i < routeList.size(); i++) {
            sub.add(1, i, i, routeList.get(i).name);
        }
        sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        int id = item.getItemId();
        if (id == 100) {
            return false;
        } else if (id == 111) {
            mWebView.clearHistory();
            mWebView.reload();
            return true;
        }
        String newUrl = routeList.get(item.getOrder()).url + mSiteUrl;
        L.e("TAG", "更换线路为：" + newUrl);
        mWebView.pauseTimers();
        mWebView.stopLoading();
        mWebView.loadUrl(newUrl);
        return true;
    }


    @Override
    protected void onNewIntent(Intent intent) {
        if (intent == null || mWebView == null || intent.getData() == null) return;
        mWebView.loadUrl(intent.getData().toString());
    }

    @Override
    protected void onResume() {
        mWebView.setJSEnabled(true);
        super.onResume();
    }

    @Override
    protected void onStop() {
        mWebView.setJSEnabled(false);
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        System.exit(0);
    }
}
